package com.easylinkin.linkappapi.crane.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.crane.dto.CraneBusinessDayStatsics;
import com.easylinkin.linkappapi.crane.dto.CraneBusinessDayStatsicsReq;
import com.easylinkin.linkappapi.crane.entity.CraneBusinessRecord;
import com.easylinkin.linkappapi.crane.service.ICraneBusinessRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import java.util.Arrays;


/**
 * 吊机业务数据记录表 前端控制器
 *
 * @author xy
 * @since 2025-06-23
 */
@Api(tags = "吊机业务数据记录表")
@RestController
@RequestMapping("/crane/craneBusinessRecord")
public class CraneBusinessRecordController {
    @Autowired
    private ICraneBusinessRecordService craneBusinessRecordService;

    /**
     * 分页列表查询
     *
     * @param requestModel
     * @return
     */
    @ApiOperation(value="分页列表查询", notes="分页列表查询")
    @PostMapping(value = "/listPage")
    public RestMessage queryPageList(@RequestBody RequestModel<CraneBusinessRecord> requestModel) {
        // Assert.notNull(requestModel,"参数不能为空");
        // Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<CraneBusinessRecord> pageList = craneBusinessRecordService.findPage(requestModel);
        return RestBuilders.successBuilder().data(pageList).build();
    }

    /**
     * 按日统计吊重吊次数
     *
     * @param record
     * @return
     */
    @ApiOperation(value="按日统计吊重吊次数", notes="按日统计吊重吊次数")
    @PostMapping(value = "/countByDay")
    public RestMessage countByDay(@RequestBody CraneBusinessDayStatsicsReq record) {
        List<CraneBusinessDayStatsics> pageList = craneBusinessRecordService.countByDay(record);
        return RestBuilders.successBuilder().data(pageList).build();
    }

    /**
     * 保存
     *
     * @param craneBusinessRecord
     * @return
     */
    @ApiOperation(value="保存", notes="保存")
    @PostMapping(value = "/save")
    public RestMessage save(@RequestBody CraneBusinessRecord craneBusinessRecord) {
        if(craneBusinessRecord.getId() != null){
            craneBusinessRecordService.updateById(craneBusinessRecord);
        } else{
            craneBusinessRecordService.save(craneBusinessRecord);
        }
        return RestBuilders.successBuilder().message("保存成功" ).build();
    }

    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value="通过id删除", notes="通过id删除")
    @DeleteMapping(value = "/delete")
    public RestMessage delete(@RequestParam("id") Integer id) {
        craneBusinessRecordService.removeById(id);
        return RestBuilders.successBuilder().message("删除成功").build();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value="批量删除", notes="批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public RestMessage deleteBatch(@RequestParam("ids") String ids) {
        this.craneBusinessRecordService.removeByIds(Arrays.asList(ids.split(",")));
        return RestBuilders.successBuilder().message("批量删除成功").build();
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value="通过id查询", notes="通过id查询")
    @GetMapping(value = "/queryById")
    public RestMessage queryById(@RequestParam("id") Integer id) {
        CraneBusinessRecord craneBusinessRecord = craneBusinessRecordService.queryById(id);
        if(craneBusinessRecord == null) {
            return RestBuilders.errorBuilder().message("未找到对应数据").build();
        }
        return RestBuilders.successBuilder().data(craneBusinessRecord).build();
    }
}
